/*
 * Author:  Brett Rettura, Natalie
 * Company:  1186 Entertainment
 * Date Modified:  3/26/07
 * Filename:  Register.java
 */

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class Register extends JPanel implements ActionListener
{

public ClientApplet CA;

//declare components
JLabel lblHeading;
JLabel lblUserName;
JLabel lblUserPwd;
JLabel lblCnfUserPwd;
JLabel lblEmail;
JLabel lblBirth;
JLabel  lblSex;

String DELIM = "&";

String usrName;
char[] usrPwd;
char[] cnfPwd;
String email;
String birth;
String gender;

JComboBox lstSex;

JTextField txtUserName;
JPasswordField txtUsrPwd;
JPasswordField txtCnfUsrPwd;
JTextField txtEmail;
JTextField txtBirth;
Font f;
Color r;
JButton btnSubmit;
JButton btnBack;

public Register(ClientApplet CA1)
{

	CA = CA1;

	//apply the layout
	this.setLayout(new BorderLayout());
	JPanel panel = new JPanel(new GridLayout(6, 2));

	//place the components
	lblHeading=new JLabel("Registration Info");
 	Font f = new Font("Dialog" , Font.BOLD , 14);
	lblHeading.setFont(f);
	Color c=new Color(10,102,255);
	lblHeading.setForeground(c);
	lblHeading.setVerticalAlignment(SwingConstants.TOP);

	this.add(lblHeading, BorderLayout.NORTH);

	//CONSTRUCT FIELDS
	lblUserName = new JLabel("Enter Username");
	panel.add(lblUserName);
	txtUserName=new JTextField(15);
	panel.add(txtUserName);

	lblUserPwd=new JLabel("Enter Password");
	panel.add(lblUserPwd);
	txtUsrPwd = new JPasswordField(15);
	panel.add(txtUsrPwd);

	lblCnfUserPwd=new JLabel("Confirm Password");
	panel.add(lblCnfUserPwd);
	txtCnfUsrPwd=new JPasswordField(15);
	panel.add(txtCnfUsrPwd);

	lblEmail=new JLabel("Email");
	panel.add(lblEmail);
	txtEmail=new JTextField(15);
	panel.add(txtEmail);

	lblBirth=new JLabel("Birthday:");
	panel.add(lblBirth);
	txtBirth=new JTextField(15);
	txtBirth.setText("yyyy-mm-dd");
	panel.add(txtBirth);

	lblSex=new JLabel("Sex");
	panel.add(lblSex);

	String[]  sex= {"male", "female"};
	lstSex=new JComboBox(sex);
	lstSex.setSelectedIndex(0);
	panel.add(lstSex);

	this.add(panel, BorderLayout.CENTER);

	//CONTRUCT BUTTONS
	JPanel btnPanel=new JPanel();

	btnSubmit=new JButton("Register");
	btnPanel.add(btnSubmit);
	btnSubmit.addActionListener(this); //add listener to the Submit button
	btnBack=new JButton("Back");
	btnPanel.add(btnBack);
	btnBack.addActionListener(this); //add listener to the Cancel button

	this.add(btnPanel, BorderLayout.SOUTH);

	//set background white
	this.setBackground(Color.WHITE);
	btnPanel.setBackground(Color.WHITE);
	panel.setBackground(Color.WHITE);

	setSize(500,500);

	setVisible(true);

}//end or Register()

public void actionPerformed(ActionEvent e1)
{


	JButton button=(JButton)e1.getSource(); //get the source of the event
	if(button.equals(btnBack))
	{
		CA.loadPanel(new Login(CA));
	}
	else
	{
		int ver=verify(); //call the verify()
		if(ver==1)
		{

		try
		{
			CA.send("3"+ DELIM + usrName + DELIM + new String(usrPwd) + DELIM + usrName + DELIM + email + DELIM + gender + DELIM + birth);

        }//end of try
		catch(Exception e)
		{
			System.out.println("Exception "+e);
		}
  		}//end of if

	}//end of else
}//end of actionPerformed()

int verify() //test the validity of the user information
{
	int ctr=0;

	try
	{

		usrName=txtUserName.getText();
		usrPwd=txtUsrPwd.getPassword();
		cnfPwd=txtCnfUsrPwd.getPassword();
		email=txtEmail.getText();
		birth=txtBirth.getText();
		String strUsrPwd=new String(usrPwd);
		String strCnfPwd=new String(cnfPwd);

		System.out.println("Does it make it here?");

		if(lstSex.getSelectedIndex() == 0)
		{
			gender = "male";
		}
		else
		{
			gender = "female";
		}


		if(strUsrPwd.equals(strCnfPwd) == false)
		{
			showErrordlgMsg("Confirmation password does not match password.");
		}

		try
		{
			String [] birthParts = birth.trim().split("-");
			int intYear = (int)Integer.parseInt(birthParts[0]);
			int intMonth = (int)Integer.parseInt(birthParts[1]);
			int intDay = (int)Integer.parseInt(birthParts[2]);

		}
		catch(Exception e)
		{
			System.out.println(e);
			showErrordlgInt();
		}
	if((usrName.length()>0)&&(strUsrPwd.length()>0)&&(strCnfPwd.length()>0)&&(email.length()>0)&&(birth.length()>0)&&(strUsrPwd.equals(strCnfPwd)))
	{
		System.out.println("done");
		ctr=1;
		return ctr;
	}
	else
	{

		showErrordlg();
	}//end of else

	}//end of try
	catch(Exception e)
	{
		e.printStackTrace();
		System.out.println("exception thrown "+e);
	}//end of catch
	return ctr;
}//end of verify()


//error msg- User Exists
void showUsrExists()
{
	JOptionPane.showMessageDialog(this,"User exists.", "Message", JOptionPane.ERROR_MESSAGE);
}

int flg=0;

//error msg- Incorrect Entry
void showErrordlg()
{
	JOptionPane.showMessageDialog(this,"Incorrect entry.", "Message", JOptionPane.ERROR_MESSAGE);
}

//error msg- Incorrect Age entered
void showErrordlgInt()
{
	JOptionPane.showMessageDialog(this,"Birthday incorrect.", "Message", JOptionPane.ERROR_MESSAGE);
}

//error msg
void showErrordlgMsg(String myMsg)
{
	JOptionPane.showMessageDialog(this,myMsg, "Message", JOptionPane.ERROR_MESSAGE);
}

//public static void main(String args[])
//{
	//new Register();
//}
}//end of class
